function [meany,stdy,flag_nan,numnan]=moments_nan(y,flag_report,sernames)
% =========================================================================
% moments_nan 
% function [meany,stdy,flag_nan,numnan]=moments_nan(y,flag_report)

% Pass a matrix Y with NANs. If FLAG_REPORT==1 print number of 
% nans for each series 
% 
% Output 
% 1) MEAN       [1 nc]
% 2) STD        [1 nc]
% 3) FLAG_NAN   [1 nc] ==1 if columns has a NAN 
% 4) NUMNAN     [1 nc] Number of NAN observations 
% 
% AJ June 29 2010 
% =========================================================================
if nargin < 2 || isempty(flag_report); 
    flag_report=0; 
end 

[nobs n]=size(y) ; 
meany =zeros(1,n); 
stdy  =zeros(1,n); 
numnan=zeros(1,n); 
flag_nan=zeros(1,n); 
for ii=1:n 
    flag_nan(ii)=any( isnan(y(:,ii)) ); 
    if flag_nan(ii)==0; 
        meany(ii)=mean(y(:,ii)); 
        stdy(ii )=std( y(:,ii)); 
    else 
        pos_nonan=find(isnan(y(:,ii))==0);
        meany(ii)=mean(y(pos_nonan,ii)); 
        stdy(ii )=std(y(pos_nonan,ii)); 
        numnan(ii)=nobs-length(pos_nonan); 
    end     
end 
if flag_report==1 
    disp('Number of NAN observations'); 
    temp=crtcell([numnan(:) meany(:) stdy(:)],sernames(:),{'Num NAN','Mean','Std'} ); 
    printcell(temp); 
end 
    





